package com.skycong.hello77.fragment;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.RotateAnimation;
import android.widget.*;
import com.skycong.hello77.R;

import java.text.SimpleDateFormat;
import java.util.Date;

public class ReFlashListView extends ListView implements AbsListView.OnScrollListener{
    View footer;//底部布局文件
    int footerHeight;//底部布局文件的高度
    int firstVisibleItem;//当前第一个可见的item的位置
    int scrollState;//listView当前滚动的状态
    boolean isRemark;//标记，当前是在最低端摁下的
    int startY;//摁下时Y的值
    int statte;//当前的状态
    final  int NONE= 0;//正常状态
    final  int PULL = 1;//提示上拉状态
    final  int RELESE = 2;//提示释放状态
    final  int REFLASHING = 3;//刷新状态
    
    public ReFlashListView(Context context) {
        super(context);
        initView(context);
    }

   

    public ReFlashListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ReFlashListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    /**
     * 初始化界面，添加顶部布局文件listView
     * */
    private void initView(Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        footer = inflater.inflate(R.layout.footer_layout,null);
       footer.measure(0,0);
        footerHeight = footer.getMeasuredHeight();
        Log.i("h","footerHeight = "+footerHeight);
        bottomPadding(-footerHeight);
        this.addFooterView(footer);
        this.setOnScrollListener(this);
    }
    /**
     * 通知父布局，占用的宽，高
     *
     * */
    private  void measureView(View view){
        ViewGroup.LayoutParams p = view.getLayoutParams();
        if(p==null){
            p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        int width = ViewGroup.getChildMeasureSpec(0,0,p.width);
        int tempHeight = p.height;
        int height;
        if(tempHeight>0){
            height = MeasureSpec.makeMeasureSpec(tempHeight,MeasureSpec.EXACTLY);
        }else{
            height = MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED);
        }
        view.measure(width,height);
    }
    /**
     * 设置footer布局下边距
     * */
    private void bottomPadding(int bottomPadding){
        footer.setPadding(footer.getPaddingLeft(),bottomPadding,footer.getPaddingRight(),footer.getPaddingTop());
        footer.invalidate();
    }


    @Override
    public void onScrollStateChanged(AbsListView absListView, int scrollState) {
          this.scrollState=scrollState;
    }

    @Override
    public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        this.firstVisibleItem = firstVisibleItem;

    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:{
                if(firstVisibleItem==0){
                    isRemark=true;
                    startY = (int) ev.getY();
                }
                break;
            }
            case MotionEvent.ACTION_MOVE:{
                onMove(ev);
                break;
            }
            case MotionEvent.ACTION_UP:{
                if(statte == RELESE){
                    statte = REFLASHING;
                    //加载最新数据
                    reflashViewByState();
//                    Object iReflashListener;
//                    iReflashListener.onReflash();
                }else if(statte==PULL){
                    statte = NONE;
                    isRemark = false;
                    reflashViewByState();
                }
                break;
            }
        }
        return super.onTouchEvent(ev);
    }


   /**
    * 移动操作的过程
    *
    * */
    private void onMove(MotionEvent ev) {
        if(!isRemark){
            return;
        }
        int tempY = (int) ev.getY();
        int space = tempY-startY;
        int bottomPadding = space - footerHeight;
        switch (statte){
            case NONE:{
                if(space>0){
                    statte=PULL;
                    reflashViewByState();
                }
                break;
            }
            case PULL:{
                bottomPadding(bottomPadding);
                if(space>footerHeight+30 && scrollState==SCROLL_STATE_TOUCH_SCROLL){
                    statte = RELESE;
                    reflashViewByState();
                }
                break;
            }
            case RELESE:{
                bottomPadding(bottomPadding);
                if(space<footerHeight+30){
                    statte=PULL;
                    reflashViewByState();

                }else if(space<=0){
                    statte = NONE;
                    isRemark = false;
                    reflashViewByState();
                }
                break;
            }
        }
    }

    /**
     * 根据当前的状态，改变界面显示
     * */
    private void reflashViewByState() {
        TextView tip = (TextView) footer.findViewById(R.id.tip);
        ImageView arrow = (ImageView) footer.findViewById(R.id.arrow);
        ProgressBar progress  = (ProgressBar) footer.findViewById(R.id.progress);
        RotateAnimation anim = new RotateAnimation(0,180,
                RotateAnimation.RELATIVE_TO_SELF,0.5f,
                RotateAnimation.RELATIVE_TO_SELF,0.5f);
        anim.setDuration(500);
        anim.setFillAfter(true);
        switch (statte){
            case NONE:{
                arrow.clearAnimation();
                bottomPadding(-footerHeight);
                break;
            }
            case PULL:{
                arrow.setVisibility(View.VISIBLE);
                progress.setVisibility(View.GONE);
                tip.setText("下拉可以刷新");
                arrow.clearAnimation();
                arrow.setAnimation(anim);
                break;
            }
            case RELESE:{
                arrow.setVisibility(View.VISIBLE);
                progress.setVisibility(View.GONE);
                tip.setText("松开可以刷新");
                arrow.clearAnimation();
                arrow.setAnimation(anim);
            }
            case REFLASHING:{
                bottomPadding(50);
                arrow.setVisibility(View.GONE);
                progress.setVisibility(View.VISIBLE);
                tip.setText("正在刷新");
                arrow.clearAnimation();
                break;
            }
        }
    }

    /**
     * 获取完数据
     * */
    public  void reflashComplete(){
        statte = NONE;
        isRemark = false;
        reflashViewByState();
        TextView lastupdatetime = (TextView) footer.findViewById(R.id.lastupdate_time);
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
        Date date = new Date(System.currentTimeMillis());
         String time = format.format(date);
        lastupdatetime.setText(time);
    }
//    public void setInterface(IReflashListener iReflashListener){
//        this.iReflashListener = iReflashListener;
//    }
  /**
   * 刷新数据接口
   * */
   public   interface  IReflashListener{
       public void onReflash();
  }
}
